package com.uni.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uni.base.RestResponse;
import com.uni.pojo.Address;
import com.uni.pojo.Role;
import com.uni.pojo.User;
import com.uni.service.RoleService;
import com.uni.service.UserService;
import com.uni.utils.PageUtil;
import com.uni.utils.ParamUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * Author:      Unirithe/Mr.Chao
 * CSDN:        https://blog.csdn.net/Unirithe
 * CreatedTime:  2022/1/28 10:46
 **/
@Log4j2
@RequiresAuthentication
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/user/info.html")
    public String toInfo(HttpSession session, ModelMap modelMap){
        // 获取当前用户的地址信息
        User user = (User) session.getAttribute("userInfo");
        String[] addr = user.getAddress().split("-");
        // 依次为 省份-城市-地区-详细地址
        if(addr!=null && addr.length == 4){
            modelMap.addAttribute("addr", new Address(addr[0], addr[1], addr[2], addr[3]));
        }

        // 标记当前页面的类型
        modelMap.addAttribute("type", "info");
        return "user";
    }

    @PutMapping("/user/update/{code}")
    @ResponseBody
    public String modifyUserImg(@PathVariable String code, String base64url){

        User user = userService.getUserByCode(code);
        int length = base64url.length();
        // 限制图片文件的大小.
        if(length / 4 / 1024 / 1024 * 3 > 2 || length > 16777215)
            return RestResponse.fail("文件过大!").toJSONString();
        // 设置图片
        user.setImg(base64url);
        return userService.updateImg(user) != null ?
                RestResponse.ok("修改成功!").toJSONString() :
                RestResponse.fail("修改失败.").toJSONString();
    }

    @PutMapping("/user/update/self")
    @ResponseBody
    public String modifyUserImg(String base64url, HttpSession session){
        int length = base64url.length();
        // 限制图片文件的大小.
        if(length / 4 / 1024 / 1024 * 3 > 2 || length > 16777215) return "文件过大";
        User user = (User) session.getAttribute("userInfo");
        user.setImg(base64url);
        return userService.updateImg(user) != null ?
                RestResponse.ok("修改成功!").toJSONString() :
                RestResponse.fail("修改失败.").toJSONString();
    }
    @PostMapping("/user/update/{code}")
    @ResponseBody
    public String updateUserInfo(@PathVariable String code, User user, Address addr) {
        user.setAddressDetail(addr);
        user.setCode(code);
        User updatedUser = userService.update(user);
        return updatedUser != null ?
                RestResponse.ok(String.format("修改用户[%s]的信息成功!", user.getCode())).toJSONString()
                : RestResponse.fail(String.format("修改用户[%s]的信息失败.", user.getCode())).toJSONString();
    }

    @PostMapping("/user/query/{code}")
//    @RequiresRoles({"管理员"})
    @ResponseBody
    public String getUserInfo(@PathVariable String code){
        User user = userService.getUserByCode(code);
        return user == null ?
                RestResponse.fail("查询失败!").toJSONString()
                : RestResponse.ok(user).toJSONString();
    }



}
